package com.shushan.pages;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.android.net.bean.NetRetBean;
import com.android.net.core.common.RequestType;
import com.android.net.helper.NetHelper;
import com.android.net.listener.common.CallbackCode;
import com.android.net.url.UrlParse;
import com.shushan.base.BaseActivity;
import com.shushan.base.Constants;
import com.shushan.base.ScreenManager;
import com.shushan.mixedexam.adapter.StudyProcessSelectAdapter;
import com.shushan.login.bean.ChildBean;
import com.shushan.network.UrlCommParse;
import com.shushan.network.UrlConst;
import com.shushan.network.listener.NetStrCommListener;
import com.shushan.pages.bean.OutlineBean;
import com.shushan.shushanbao.BabyApplication;
import com.shushan.shushanbao.R;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: StudyProgressSetActivity
 * @Desciption: //学习进度设置页面
 * @author: zhangshihao
 * @date: 2018-04-12
 */
public class StudyProgressSetActivity extends BaseActivity implements View.OnClickListener, OnOutlineSelectListener {

    private TextView tvSubjectStudyProgress;
    private TextView tvLevelTerm;
    private TextView tvBookVersion;
    private ImageView backImageView;

    private ListView lvLessons;
    private StudyProcessSelectAdapter studyProcessSelectAdapter;

    private BabyApplication shuShanApplication = BabyApplication.getInstance();
    private String pressName;
    private int outlineDetailId;
    private int outlineId;
    private int subjectId;
    private int practiceType;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_study_progress_set);

        ScreenManager sm = new ScreenManager();
        sm.setStatusBar(true,this);

        pressName = getIntent().getStringExtra("pressName");
        outlineDetailId = getIntent().getIntExtra("outlineDetailId",0);
        outlineId = getIntent().getIntExtra("outlineId",0);
        subjectId = getIntent().getIntExtra("subjectId",0);
        practiceType = getIntent().getIntExtra("practiceType",0);

        initView();

        initData();

        getOutlines();
    }

    private void initView(){
        tvSubjectStudyProgress = (TextView) findViewById(R.id.tv_subject_study_progress);
        tvLevelTerm = (TextView) findViewById(R.id.tv_level_term);
        tvBookVersion = (TextView) findViewById(R.id.tv_book_version);
        lvLessons = (ListView) findViewById(R.id.lv_lessons);
        backImageView = findViewById(R.id.iv_back);
        backImageView.setOnClickListener(this);
    }

    private void initData() {
        if(!TextUtils.isEmpty(pressName)){
            tvBookVersion.setText(pressName);
        }

        if(subjectId == Constants.SUBJECT_CHINESE_ID){
            tvSubjectStudyProgress.setText("语文学习进度");
        }else if(subjectId == Constants.SUBJECT_MACH_ID){
            tvSubjectStudyProgress.setText("数学学习进度");
        }else if(subjectId == Constants.SUBJECT_ENGLISH_ID){
            tvSubjectStudyProgress.setText("英语学习进度");
        }

        ChildBean childBean = shuShanApplication.getCurrentChildBean();
        if(childBean != null){
            tvLevelTerm.setText(childBean.gradeName + " "
                    + (("UP").equals(childBean.semester)?"上学期":"下学期"));
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_back:
                onBackAction();
                break;
            default:
                break;
        }
    }

    @Override
    public void onOutlineSelected(int resultCode) {
        onBackAction(resultCode);
    }


    private void getOutlines() {
        int childId = shuShanApplication.getCurrentChildId();
        showWaitDialog();
        String region;
        if(practiceType == Constants.TYPE_ORAL_CALCULATE_EXERCISE){
            region = UrlConst.OC_PROGRESS_LIST;
        }else{
            region = UrlConst.GET_PROGRESS_LIST;
        }
        UrlParse urlParse = new UrlCommParse(this, UrlConst.BASE_URL).appendRegion(region);
        UrlParse paramParse = new UrlCommParse(this)
                .putValue("child_id", childId)
                .putValue("outline_id", outlineId);

        NetHelper.create()
                .url(urlParse.toStringOnlyHeader())
                .param(paramParse.toStringOnlyParam())
                .request(RequestType.REQUEST_TYPE_POST, new NetStrCommListener() {
                    @Override
                    protected void onCommon() {
                        dismissWaitDialog();
                    }

                    @Override
                    protected void onSuccess(String data) {
                        List<OutlineBean> outlineList = new ArrayList<OutlineBean>();

                        try {
                            JSONObject jsonObject = new JSONObject(data);

                            JSONArray jsonArray = jsonObject.optJSONArray("tree");
                            for (int i = 0;i < jsonArray.length();i++){
                                JSONObject json = jsonArray.optJSONObject(i);

                                OutlineBean outlineBean = new OutlineBean();
                                outlineBean.initByJson(json);
                                outlineList.add(outlineBean);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                        if(outlineList != null && outlineList.size() > 0){
                            if(practiceType == Constants.TYPE_ORAL_CALCULATE_EXERCISE){
                                studyProcessSelectAdapter = new StudyProcessSelectAdapter(StudyProgressSetActivity.this,
                                        subjectId,outlineId,outlineList,true);
                            }else {
                                studyProcessSelectAdapter = new StudyProcessSelectAdapter(StudyProgressSetActivity.this,
                                        subjectId, outlineId, outlineList);
                            }
                            studyProcessSelectAdapter.setOnOutlineSelectListener(StudyProgressSetActivity.this);
                            studyProcessSelectAdapter.setSelectId(outlineDetailId);
                            studyProcessSelectAdapter.setPracticeType(practiceType);
                            lvLessons.setAdapter(studyProcessSelectAdapter);
                        }

                    }

                    @Override
                    protected void onError(CallbackCode errorCode, NetRetBean netRetBean) {
                        showNetError(netRetBean);
                    }
                });
    }

}
